<?php

namespace App\Http\Controllers\Company;

use App\Facades\Style;
use App\Http\Controllers\Company\CommonController;
use App\Http\Requests\Company\Register\CompanyBasicRequest;
use App\Models\Common\CollArea;
use App\Models\Common\CollOption;
use App\Models\Company\CompanyBasic;
use App\Models\Company\CompanyJob;
use Illuminate\Foundation\Auth\User;
use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;


class BasicController extends CommonController{

    /**
     * 基本资料填写
     * @param Request $request
     * @return mixed
     */
    public function info(Request $request){
        $userInfo = auth()->guard('company')->user();
        $CompanyBasic = CompanyBasic::where('uid', $userInfo->uid)->orderBy('updatetime', 'desc')->first();
        //企业不存在时
        if(empty($CompanyBasic)){
            return redirect("company/register_basic");
        }
        $phoneArray = explode('-', $CompanyBasic->phone);
        if(!empty($phoneArray[0])) $CompanyBasic->area_code = $phoneArray[0];
        if(!empty($phoneArray[1])) $CompanyBasic->phone = $phoneArray[1];
        if(!empty($phoneArray[2])) $CompanyBasic->ext = $phoneArray[2];
        return Style::view('company.basicInfo', compact('CompanyBasic'));
    }

    public function postInfo(CompanyBasicRequest $request){
        $input = $request->only('company_name', 'brands', 'comkind', 'industry', 'established', 'employee_num', 'temptation', 'profile',
            'contacts', 'mobile', 'phone', 'fax', 'email', 'qq', 'region', 'address', 'longitude', 'latitude', 'coordinate_address', 'website', 'lines');
        $phone = trim($request->input('area_code')) . '-' . trim($request->input('phone'));
        $phone .= trim($request->input('ext')) ? "-" . trim($request->input('ext')) : '';
        $phone = trim($phone, '-');
        //获取用户信息ID
        $userInfo = auth()->guard('company')->user();
        $input['phone'] = $phone;
        //$input['established'] = date("Y-m-d", strtotime($request->established));
        $input['updatetime'] = date('Y-m-d H:i:s', time());

        if($request->input('id')){
            $companyBasic = CompanyBasic::where('uid', $userInfo->uid)->where('id', $request->input('id'))->count();
            if($companyBasic){
                CompanyBasic::where('uid', $userInfo->uid)->where('id', $request->input('id'))->update($input);
                CompanyJob::where('uid',$userInfo->uid)
                    ->where('com_id',$request->input('id'))->where('job_status',1)
                    ->update(['xs_status'=>0]);
                return redirect('company/basic_info')->with('ok', "保存成功");
            }else{
                return Style::view('errors.404', ['exception' => '错误未知id...'], 404);
            }
        }else{
            $input['uid'] = $userInfo->uid;
            CompanyBasic::create($input);
            return redirect('company/welcome');
        }
    }


    /**
     * logo上传
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function logoUpload(Request $request){
        if($request->hasFile('file')){
            $file = $request->file('file');
            $entension = strtolower($file->getClientOriginalExtension());
            //$mimeTye = $file -> getMimeType();
            $clientName = $file->getClientOriginalName();//客服端文件名
            if($file->isValid()){
                if(!in_array($entension, array('jpg', 'gif', 'png'))){
                    return response()->json(['error' => 304, 'info' => "文件类型只能为jpg、gif、png！"]);
                }else{
                    $userInfo = auth()->guard('company')->user();
                    $company_basic = CompanyBasic::where('uid', $userInfo->uid)->orderBy('updatetime', 'desc')->first(['id', 'logo']);
                    if(!$company_basic){
                        return response()->json(['error' => 404, 'info' => "没有找到基本信息！"]);
                    }
                    if($company_basic->logo){
                        @unlink(public_path() . '/upload/companylogo/' . $company_basic->logo);
                    }
                    $fileurl = date("Ym") . '/';
                    $filename = time() . strtolower(str_random(5)) . '.' . $entension;
                    $request->file('file')->move(public_path() . '/upload/companylogo/' . $fileurl, $filename);

                    $company_basic->logo = $fileurl . $filename;
                    $company_basic->save();

                    return response()->json(['status' => 200, 'url' => asset('upload/companylogo/' . $fileurl . $filename)]);
                }
            }
        }else{
            return response()->json(['error' => 306, 'info' => "没有选择上传图片文件,或者文件过大！"]);
        }
    }

    /**
     * 设置地图
     * @return mixed
     */
    public function map(){
        return Style::view('company.map');
    }

    public function mapUpdate(Request $request){
        $userInfo = auth()->guard('company')->user();
        $companyBasic = CompanyBasic::where('uid', $userInfo->uid)->where('id', $request->id)->first();
        if(!empty($companyBasic)){
            $llc = explode(",", $request->mapposition);
            $longitude = empty($llc['0']) ? '' : $llc['0'];
            $latitude = empty($llc['1']) ? '' : $llc['1'];
            $coordinate_address = empty($llc['2']) ? '' : $llc['2'];
            $companyBasic->longitude = $longitude;
            $companyBasic->latitude = $latitude;
            $companyBasic->coordinate_address = $coordinate_address;
            $companyBasic->save();
            return response()->json(['status' => 200]);
        }
        return response()->json(['status' => 501]);
    }

    /**
     * 设地地图frame
     * @return mixed
     */
    public function setMapFiframe(){
        $userInfo = auth()->guard('company')->user();
        $CompanyBasic = CompanyBasic::where('uid', $userInfo->uid)->orderBy('updatetime', 'desc')->first(['id', 'longitude', 'latitude', 'coordinate_address']);
        return Style::view('company.setMapFiframe', compact('CompanyBasic'));
    }

    /**
     * 跳转让到招聘主页
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function goHome(){
        $userInfo = auth()->guard('company')->user();
        $CompanyBasic = CompanyBasic::where('uid', $userInfo->uid)->orderBy('updatetime', 'desc')->first(['id']);
        if(!empty($CompanyBasic)){
            return redirect('comabout_' . idEncryption($CompanyBasic->id) . '.html');
        }else{
            return redirect('/company/register_basic');
        }
    }


    /**
     * 微招聘
     * @param $qrCodeLink
     * @param $head_title
     * @return string
     */
    public function micro(Request $request){
        if(!empty($request->com_id)){
            $url = url('micro_recruitment_' . idEncryption($request->com_id) . '.html');
            $q = QrCode::format('png')->size(200)->generate($url);
            return response($q, 200, [
                'Content-Type' => 'image/png',
            ]);
        }
    }

}